package com.core.okhttpcore;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author 苏斌
 * @date 2021-7-23
 * http 设置类
 */

/*
@ConditionalOnBean（仅仅在当前上下文中存在某个对象时，才会实例化一个Bean）
@ConditionalOnClass（某个class位于类路径上，才会实例化一个Bean）
@ConditionalOnExpression（当表达式为true的时候，才会实例化一个Bean）
@ConditionalOnMissingBean（仅仅在当前上下文中不存在某个对象时，才会实例化一个Bean）
@ConditionalOnMissingClass（某个class类路径上不存在的时候，才会实例化一个Bean）
@ConditionalOnNotWebApplication（不是web应用）

 */
@Configuration
@EnableConfigurationProperties(OkHttpProperties.class)
public class OkHttpConfiguration {


    /**
     * 默认自动注册一个，如需要多个，手工new第二个实力再注入（比如需要代理ip)
     * @return
     */
    @Bean
    @Primary
    @ConditionalOnProperty(prefix = "okhttputil",name = "enable",havingValue = "true")
    @ConditionalOnBean(value = OkHttpProperties.class)
    @ConditionalOnMissingBean(value = OKHttpUtilMap.class)
    public OKHttpUtilMap oKHttpUtilMap(OkHttpProperties okHttpProperties) {
        Logger logger = LoggerFactory.getLogger("OkHttpUtil数据源");
        OKHttpUtilMap ok = new OKHttpUtilMap();
        for (Config config:
                okHttpProperties.getConfigs()) {
            ok.AddDataSource(config);
            logger.info("注入: "+config.getName()+" 源");
        }
        return  ok;
    }

    /**
    @Bean
    public X509TrustManager x509TrustManager() {
        return new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }
            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        };
    }
    @Bean
    public SSLSocketFactory sslSocketFactory() {
        try {
            // 信任任何链接
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{x509TrustManager()}, new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            e.printStackTrace();
        }
        return null;
    }
*/

    /**
     * Create a new connection pool with tuning parameters appropriate for a single-user application.
     * The tuning parameters in this pool are subject to change in future OkHttp releases. Currently

    @Bean
    public ConnectionPool pool() {
        return new ConnectionPool(200, 5, TimeUnit.MINUTES);
    }
 */
}
